﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SmoothMove : MonoBehaviour {

    //这里建议 把 三维坐标改成  上升的  value      value  == ui.height
    public Vector3 mMoveTarLocalPos;
    Vector3 mInitPos;
    public float _Speed = 100;

    Vector3 mTargetPos;
    bool mIsMove;
    RectTransform myTransfom;
	// Use this for initialization
	void Start () {
        myTransfom = GetComponent<RectTransform>();
        mInitPos = myTransfom.anchoredPosition;
	}
	
	// Update is called once per frame
	void Update ()
    {
        Move();
    }

    void Move()
    {
        if (mIsMove)
        {
            //if (Vector3.Distance(transform.localPosition, mTargetPos) > 0.1f)
            //    myTransfom.localPosition = Vector3.MoveTowards(myTransfom.localPosition, mTargetPos, _Speed*100 * Time.deltaTime);
            //else
            //{
            //    mIsMove = false;
            //}

            if (Vector2.Distance(myTransfom.anchoredPosition, mTargetPos) > .1f)
            {
                myTransfom.anchoredPosition = Vector2.Lerp(myTransfom.anchoredPosition, mTargetPos, Time.deltaTime * _Speed);
            }
            else
            {
                myTransfom.anchoredPosition = mTargetPos;
                mIsMove = false;
            }
        }
    }

    public void StartMove()
    {
        if (mIsMove) return;
        mTargetPos = mInitPos;
        mIsMove = true;
    }

    public void CloseMove()
    {
        if (mIsMove) return;
        mTargetPos = mMoveTarLocalPos;
        mIsMove = true;
    }
}
